package com.study.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.study.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @ClassName: SpringMVCConfig
 * @Description: springmvc配置文件
 * @author: WS
 * @date 2020/12/8
 * @Version: 1.0
 **/
@Configuration
@ComponentScan(basePackages = {"com.study"},
        useDefaultFilters = true,
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)
        })
public class SpringMVCConfig extends WebMvcConfigurationSupport {
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        }

        @Override
        protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
        }
        @Bean
        MyInterceptor myInterceptor(){
                return new MyInterceptor();
        }

        @Override
        protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();
                converters.add(converter);
        }
}
